﻿#include "form.h"
#include "ui_form.h"

Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);
     setWindowFlags(Qt::Widget);// 使右上角最小化等三个按钮 都可使用
     html = new http(this);

}

//todo:
//设置开机自启
//设置计时器
//设置中文输出和提示

Form::~Form()
{
    delete ui;
}

void Form::on_btn_search_clicked()
{
    ui->textEdit->clear();

    //组合查询网址

    QDateTime time = QDateTime::currentDateTime();//获取当前时间
    int timeT = time.toTime_t();//将当前时间转为时间戳
    QString base_Url=QString("http://www.ceic.ac.cn/ajax/speedsearch?num=1&&page=1&&callback=jQuery18008120960236025978_%1&_=%1").arg(timeT);
    qDebug()<<base_Url;
    QUrl url = QUrl(base_Url);
    QUrlQuery query;
    QFile file("result_1.html");
    file.remove();
    html->Get(url,QString("result_1.html"));

    if(!file.open(QIODevice::ReadWrite | QIODevice::Text)){
        return;
    }
    QTextStream stream(&file);
    QString data = stream.readAll();
    QStringList numList=search("number=[0-9]+",data);
}

void Form::getData(QString filename)
{
    ui->textEdit->clear();
    QFile file(filename);
    if(!file.open(QIODevice::ReadWrite | QIODevice::Text))
    {
        return;
    }
    QTextStream stream(&file);
    QString data=stream.readAll();
    //QStringList latList=search(":\"&-?(([1-9]\\d*\\.\\d*)|(0\\.\\d*[1-9]\\d*)|([1-9]\\d*))",data);

    //QStringList timeList = search("[0-9]+:+[0-9]+:+[0-9]{0,2}",data);
    QStringList timeList = search("\"O_TIME\":\"[0-9]+-+[0-9]+-+[0-9]{0,2}+\\s+[0-9]+:+[0-9]+:+[0-9]{0,2}",data);//转义符需要用两个斜杠
    QStringList latList = search("LAT\":\"-?([0-9]{0,3}+\\.+[0-9]{0,2})",data);
    QStringList lonList = search("LON\":\"-?([0-9]{0,3}+\\.+[0-9]{0,2})",data);
    QStringList levelList = search("\"M\":\"-?(([1-9]\\d*\\.\\d*)|(0\\.\\d*[1-9]\\d*)|([1-9]\\d*))",data);
    //todo：输出处理

    QString output;
    for(int i =0;i < timeList.size();i++)
    {
        QString f_date = timeList[i].replace("\"O_TIME\":\"","");//要用正则表达式
        QString f_lat = latList[i].replace("LAT\":\"","");
        QString f_lon = lonList[i].replace("LON\":\"","");
        QString f_level = levelList[i].replace("\"M\":\"","");


        output = "date:" + f_date + " " + "lat:" + f_lat + " lon:" + f_lon + " level:" + f_level;
        ui->textEdit->append(output);
    }
    //ui->textEdit->setText(data);
    timeList.clear();
    qDebug()<<timeList;
    latList.clear();
}

QStringList Form::search(QString regularexp,QString data){
    QStringList searchdata;
    QRegularExpression regularExpression(regularexp);
    int index = 0;
    QRegularExpressionMatch match;
    do {
        match = regularExpression.match(data, index);
        if(match.hasMatch()) {
            index = match.capturedEnd();
            searchdata.append(match.captured(0));
        }
        else{
            break;
        }
    } while(index < data.length());
    qDebug()<<match.captured(0);
    qDebug()<<searchdata;
    return searchdata;
} //这个函数参考了https://blog.csdn.net/d7185540/article/details/75041289

void Form::on_btn_data_clicked()
{
    getData("result_1.html");
}
